package com.qf.controller;

import com.qf.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @RequestMapping:可以标记在类上或者方法上，映射前段路径
 * 标记在类上，请求方法时 都需要添加此路径
 * 标记在方法上，请求方法时 需要添加此路径
 *
 * 如果添加redirect || forward前缀，视图解析器的前缀和后缀不生效
 * WEB-INF下的资源  必须是 请求转发才能访问，重定向不可以访问
 *
 * redirect:表示重定向
 * forward：请求转发
 */
@Controller
@RequestMapping("test")
@SessionAttributes({"a","user"})
public class TestController {
    @RequestMapping("hello")
    public String test(){
        return "redirect:/hello.jsp";
    }
    @RequestMapping("/getData01")
    public String getData01(HttpServletRequest request, HttpSession session, HttpServletResponse response){
        request.setAttribute("a","Hello");
        session.setAttribute("b","hello-session");

        return "redirect:/hello.jsp";
    }
    @RequestMapping("/getData02")
    public String getData02(Model model){
        model.addAttribute("a","kong");
        User user=new User();
        user.setId(23);
        user.setUsername("wenliang");
        user.setPassword("111");
        model.addAttribute("user",user);

        return "forward:/hello.jsp";
    }
    @RequestMapping("/getData03")
    public ModelAndView getData03(){
        ModelAndView mav=new ModelAndView();
        //在请求域中添加数据
        mav.addObject("a","kong");
        User user=new User();
        user.setId(23);
        user.setUsername("wenliang");
        user.setPassword("111");
        mav.addObject("user",user);
        //指定跳转页面
        mav.setViewName("forward:/hello.jsp");

        return mav;
    }
    @RequestMapping("/getData04")
    public String getData04(Model model){
        model.addAttribute("a","kong");
        User user=new User();
        user.setId(23);
        user.setUsername("wenliang");
        user.setPassword("111");
        model.addAttribute("user",user);

        return "redirect:/hello.jsp";
    }

}
